home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / VideoToolbox / VideoToolboxSources / SetPixelsQuickly.c < prev    next >
Text File  |  1993-12-14  |  21KB  |  631 lines

  1. /*
  2. SetPixelsQuickly.c
  3. All the Set... routines poke a row of pixels.
  4. All the Get... routines peek a row of pixels.
  5. SetPixelsQuickly and GetPixelsQuickly use the current port.
  6. SetWindowPixelsQuickly and GetWindowPixelsQuickly use the supplied window.
  7. SetDevicePixelsQuickly and GetDevicePixelsQuickly use the supplied video device.
  8. SetPixmapPixelsQuickly and GetPixmapPixelsQuickly use the supplied pix/bitmap.
  9.  
  10. int SetPixelsQuickly(int x,int y,unsigned long value[],short n);
  11. int GetPixelsQuickly(int x,int y,unsigned long value[],short n);
  12. int SetWindowPixelsQuickly(WindowPtr window,int x,int y,unsigned long value[],short n);
  13. int GetWindowPixelsQuickly(WindowPtr window,int x,int y,unsigned long value[],short n);
  14. int SetDevicePixelsQuickly(GDHandle device,int x,int y,unsigned long value[],short n);
  15. int GetDevicePixelsQuickly(GDHandle device,int x,int y,unsigned long value[],short n);
  16. int SetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[],short n);
  17. int GetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[],short n);
  18.  
  19. This is the fastest and easiest way to transform back and forth between a PixMap
  20. or BitMap and a numerical representation of an arbitrary image amenable to
  21. computation in C. You can think of these routines as replacements for the
  22. official Apple SetCPixel, etc. Their virtue is that they're very fast and don't
  23. translate your index via any color table. Set... and Get... ignore the color
  24. spec arrays, giving you direct access to the unsigned number stored in each
  25. pixel (whether 1, 2, 4, 8, 16, or 32 bits). With appropriate casting you may
  26. supply a CWindowPtr in place of the WindowPtr, and you may supply a BitMapPtr in
  27. place of the PixMapPtr.
  28.  
  29. For all the routines, you supply the (x,y) coordinate of the first pixel you
  30. want to access in the appropriate coordinate system, an unsigned long array
  31. called "value", and the number of pixels to access. x will increment for each
  32. successive pixel. Set... will copy values, one by one, from "value" into the
  33. pix/bitmap. Get... will copy from the pix/bitmap into "value". The routines work
  34. with all pixel sizes: 1 to 32 bits. The "value" array that you supply is always
  35. unsigned long.
  36.  
  37. All the routines clip to enforce either the pix/bitmap's bounds, or the window's
  38. portRect (if you pass a window or use the current window/port). The only way of
  39. getting into trouble would be to pass a window's PixMap directly. It is
  40. difficult to figure out the clipping of a window's PixMap; RectToAddress does
  41. it, but only on the first call, after which we use the old cached answer, which
  42. doesn't include clipping information.
  43.  
  44. The returned value of the function is normally zero, indicating success, but
  45. will be nonzero if the request could not be fully satisfied. All the routines
  46. clip to the bit/pixmap bounds; this is treated as normal by the Set... routines
  47. and is reported as an error by the Get... routines. In the latter case some of
  48. the elements of the value array will have been left untouched because the pixels
  49. they correspond to could not be accessed.
  50.  
  51. These routines (and RectToAddress) do not "move memory", i.e. they don't give
  52. the Memory Manager any pretext for shuffling around the memory allocations and
  53. changing its master pointers. That's why it's OK to dereference the pixmap handle,
  54. passing as an argument a temporary copy of the pixmap's master pointer.
  55.  
  56. SetPixmapPixelsQuickly and GetPixmapPixelsQuickly run fast by caching the
  57. information that they get about your Bit/Pixmap from RectToAddress. Each has its
  58. own cache. The cache is assumed to be fresh (i.e. is not recomputed) if it
  59. receives the same Pix/Bitmap as last time. It checks whether the pix/bitmap has
  60. the same address, baseAddr, rowBytes, and bounds. However, if the pix/bitmap
  61. has the same baseAddr as the mainDevice then the cache is not used, because
  62. in a multi-screen environment that pix/bitmap is likely to be sited on another
  63. device, which MAY have changed. You can force a flush of the
  64. cache by passing a NULL bit/pixmap pointer. (This will result in a returned
  65. value of 0, since the flush is always successful.)
  66.  
  67. See the demo Grating.c for an example of how to use this to display a pattern on
  68. the screen.
  69.  
  70.     // This is a simple write-then-read test of these routines,
  71.     // writing random numbers to the top line of the main screen.
  72.     unsigned long row[100],row2[100];
  73.     int rowLength=100,clutSize,i;
  74.     device=GetMainDevice();
  75.     clutSize=GDClutSize(device);
  76.     for(i=0;i<rowLength;i++)row[i]=nrand(clutSize);
  77.     SetDevicePixelsQuickly(device,0,0,row,rowLength);
  78.     GetDevicePixelsQuickly(device,0,0,row2,rowLength);
  79.     for(i=0;i<rowLength;i++)if(row2[i]!=row[i])
  80.         printf("%d-th pixel: wrote %ld, but read %ld\n",i,row[i],row2[i]);
  81.  
  82. HISTORY:
  83. 4/4/89     dgp wrote SetIPixel.c
  84. 9/8/90     dgp updated to work with 32 bit QuickDraw, if present.
  85. 10/15/90 bf renamed to SetIPixelGW.c and modified for drawing to off screen pix maps.
  86. 4/26/92     dgp Merged the two variants: SetIPixel.c and SetIPixelGW.c to produce the
  87.             new file SetOnePixel.c. Renamed existing routines to SetPixmapPixel,
  88.             GetPixmapPixel,SetDevicePixel,GetDevicePixel. Added SetOnePixel and 
  89.             GetOnePixel. Generalized to handle any pixelSize, and accept bitmaps as well
  90.             as pixmaps.
  91. 12/23/92 dgp Doubled the speed of SetPixmapPixel and GetPixmapPixel (and thus sped up
  92.             all the routines that call them) by caching the answers from RectToAddress. 
  93. 1/6/93 dgp    Fixed tiny but disastrous bug in GetPixmapPixel (wasn't saving old x and y).
  94. 1/22/93    dgp    Check more PixMap fields to make sure cache is not stale.
  95. 2/7/93    dgp Wrote SetPixelsQuickly.c
  96. 2/8/93    dgp Ironed out some wrinkles in the clipping.
  97. 4/27/93    dgp    Invalidate cache if baseAddr==mainDeviceBaseAddr.
  98. 6/4/93     dhb Modified to deal with MEX file weirdness in 24-bit mode.
  99.             These are all conditionally compiled in with the MATLAB
  100.             symbol. No intentional changes to original.
  101.         dgp    Here's my attempt to explain the problem and solution. 
  102.             MATLAB puts garbage in the high byte of the program counter register, 
  103.             so the machine crashes the instant you switch into 32-bit mode. 
  104.             The fix is to call a subroutine whose address has been cleaned by calling
  105.             StripAddress. This puts a 32-bit-clean address into the program counter,
  106.             until the subroutine returns. 
  107.         dhb The SwapMMUMode calls are arranged so that when MATLAB is defined only
  108.             a minimal amount of code is run in 24 bit mode.  The problem is that 
  109.             register A4, which is used in global addressing, also has garbage in
  110.             the high byte.  Not only is A4 used to reference globals, but as far
  111.             as I could tell, it is also used as an offset in certain code jumps. 
  112.             So if it is wrong, all hell breaks loose.  The solution is to run only
  113.             the code that blits the image in 32 bit mode.  I expect that this problem
  114.             will be fixed by a future version of MATLAB.
  115.         dgp    THINK C 5 sometimes implemented "switch" by a subroutine call, that might
  116.             explain the need to put the SwapMMUMode calls inside the switch, since
  117.             any subroutine call will fail when the global address register contains
  118.             garbage.
  119. 6/23/93    dgp Used THINK C preprocessor and MPW Compare to confirm that
  120.             code is unchanged when MATLAB is false.
  121. 7/9/93    dgp    Replaced calls to QD32Exists() by the variable can32, which is quicker,
  122.             and based on a more appropriate test: gestalt32BitCapable.
  123.             Test MATLAB in if() instead of #if. This is easier to read and
  124.             the compiler is smart enough to evaluate it at compile time so there's 
  125.             no runtime penalty.
  126. */
  127. #include "VideoToolbox.h"
  128. #define USE_CACHE 1    // set to zero to disable cache
  129.  
  130. int SetPixelsQuickly(int x,int y,unsigned long value[],short n)
  131. // (x,y) is in the local coordinate system of the current port.
  132. {
  133.     WindowPtr window;
  134.  
  135.     GetPort(&window);
  136.     return SetWindowPixelsQuickly(window,x,y,value,n);
  137. }
  138.  
  139. int GetPixelsQuickly(int x,int y,unsigned long value[],short n)
  140. // (x,y) is in the local coordinate system of the current port.
  141. {
  142.     WindowPtr window;
  143.  
  144.     GetPort(&window);
  145.     return GetWindowPixelsQuickly(window,x,y,value,n);
  146. }
  147.  
  148. int SetWindowPixelsQuickly(WindowPtr window,int x,int y,unsigned long *value,short n)
  149. // (x,y) is in the local coordinate system of the window.
  150. // Accepts either WindowPtr or CWindowPtr.
  151. {
  152.     PixMapPtr pm;
  153.     Rect r;
  154.     
  155.     if(window==NULL)return 1;
  156.     
  157.     // Clip to portRect.
  158.     SetRect(&r,x,y,x+n,y+1);
  159.     if(!SectRect(&window->portRect,&r,&r))return 0;
  160.     value+=r.left-x;
  161.     x=r.left;
  162.     n=r.right-r.left;
  163.     
  164.     // Is it a CGrafPort or a GrafPort?
  165.     if(((CGrafPtr)window)->portVersion<0)        // It's a CGrafPort, pass pixmap ptr.
  166.         pm = *((CGrafPtr)window)->portPixMap;
  167.     else                                         // It's a GrafPort, pass bitmap ptr.
  168.         pm = (PixMapPtr) &window->portBits;
  169.     return SetPixmapPixelsQuickly(pm,x,y,value,n);
  170. }
  171.  
  172. int GetWindowPixelsQuickly(WindowPtr window,int x,int y,unsigned long *value,short n)
  173. // (x,y) is in the local coordinate system of the window.
  174. // Accepts WindowPtr or CWindowPtr.
  175. {
  176.     PixMapPtr pm;
  177.     int error=0;
  178.     Rect r;
  179.  
  180.     if(window==NULL)return 1;
  181.     
  182.     // Clip to portRect.
  183.     SetRect(&r,x,y,x+n,y+1);
  184.     if(!SectRect(&window->portRect,&r,&r))return 1;
  185.     if(x!=r.left || x+n!=r.right){    // Update after clipping.
  186.         error=1;
  187.         value+=r.left-x;
  188.         x=r.left;
  189.         n=r.right-r.left;
  190.     }
  191.     
  192.     // Is it a CGrafPort or a GrafPort?
  193.     if(((CGrafPtr)window)->portVersion<0)        // It's a CGrafPort, pass pixmap ptr.
  194.         pm = *((CGrafPtr)window)->portPixMap;
  195.     else                                         // It's a GrafPort, pass bitmap ptr.
  196.         pm = (PixMapPtr) &window->portBits;
  197.     error|=GetPixmapPixelsQuickly(pm,x,y,value,n);
  198.     return error;
  199. }
  200.  
  201. int SetDevicePixelsQuickly(GDHandle device,int x,int y,unsigned long value[],short n)
  202. // (x,y) is relative to the upper left hand corner of the screen.
  203. {
  204.     Rect r;
  205.     
  206.     if(device==NULL)return 1;
  207.     x+=(*(*device)->gdPMap)->bounds.left;
  208.     y+=(*(*device)->gdPMap)->bounds.top;
  209.  
  210.     // Clip to device bounds.
  211.     SetRect(&r,x,y,x+n,y+1);
  212.     if(!SectRect(&(*(*device)->gdPMap)->bounds,&r,&r))return 0;
  213.     value+=r.left-x;    // Update after clipping.
  214.     x=r.left;
  215.     n=r.right-r.left;
  216.     
  217.     return SetPixmapPixelsQuickly(*(*device)->gdPMap,x,y,value,n);
  218. }
  219.  
  220. int GetDevicePixelsQuickly(GDHandle device,int x,int y,unsigned long value[],short n)
  221. // (x,y) is relative to the upper left hand corner of the screen.
  222. {
  223.     int error=1;
  224.     Rect r;
  225.     
  226.     if(device==NULL)return 1;
  227.     x+=(*(*device)->gdPMap)->bounds.left;
  228.     y+=(*(*device)->gdPMap)->bounds.top;
  229.     
  230.     // Clip to device bounds.
  231.     SetRect(&r,x,y,x+n,y+1);
  232.     if(!SectRect(&(*(*device)->gdPMap)->bounds,&r,&r))return 1;
  233.     if(x!=r.left || x+n!=r.right){    // Update after clipping.
  234.         error=1;
  235.         value+=r.left-x;
  236.         x=r.left;
  237.         n=r.right-r.left;
  238.     }
  239.     error|=GetPixmapPixelsQuickly(*(*device)->gdPMap,x,y,value,n);
  240.     return error;
  241. }
  242.  
  243. #if MATLAB
  244. int SetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  245.     ,register short n)
  246. {
  247.   int (*goSet) (PixMapPtr pmPtr,int x,int y,unsigned long value[],register short n);
  248.   int SPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  249.       ,register short n);
  250.  
  251.   goSet = (void *) StripAddress(&SPixmapPixelsQuickly);
  252.   return( (*goSet) (pmPtr,x,y,value,n) );
  253. }
  254. static int SPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  255.     ,register short n)
  256. #else
  257. int SetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  258.     ,register short n)
  259. #endif
  260. // Pokes a row of pixels. Accepts either pixmap or bitmap pointer.
  261. // (x,y) is in the coordinate system of the bit/pixmap.
  262. // Speed is enhanced by reusing the cached information from last time if it's the
  263. // same Pix/Bitmap as last time, i.e. same address, baseAddr, rowBytes, and bounds.
  264. // You can flush this cache by passing a NULL bit/pixmap pointer.
  265. {
  266.     static PixMapPtr oldPmPtr=(PixMapPtr)-1;
  267.     static int oldX,oldY;
  268.     static short rowBytes,logPixelSize,bitsOffset;
  269.     static unsigned char *pixelPtr;
  270.     static BitMap oldMap;
  271.     static Ptr mainBaseAddr=NULL;
  272.     static Boolean can32=0;
  273.     char mode32=true32b;
  274.     int shift,error=0;
  275.     unsigned char mask;
  276.     Rect r;
  277.     
  278.     // Clip to pix/bitmap bounds.
  279.     SetRect(&r,x,y,x+n,y+1);
  280.     if(mainBaseAddr==NULL){
  281.         if(QD8Exists())mainBaseAddr=(*(*GetMainDevice())->gdPMap)->baseAddr;
  282.         else mainBaseAddr=(void *)-1;
  283.     }
  284.     // Clip unless the pixmap pretends to be the main device.
  285.     if(pmPtr!=NULL && pmPtr->baseAddr!=mainBaseAddr 
  286.         && !SectRect(&pmPtr->bounds,&r,&r))return 0;    // go home if we're done
  287.  
  288.     if(!USE_CACHE
  289.         || pmPtr->baseAddr==mainBaseAddr // tricky, don't trust cache
  290.         || pmPtr!=oldPmPtr
  291.         || pmPtr->baseAddr!=oldMap.baseAddr 
  292.         || pmPtr->rowBytes!=oldMap.rowBytes 
  293.         || *(long *)&pmPtr->bounds.top!=*(long *)&oldMap.bounds.top
  294.         || *(long *)&pmPtr->bounds.bottom!=*(long *)&oldMap.bounds.bottom){
  295.         // Cache is stale. Get fresh values.
  296.         short pixelSize;
  297.         long value=0;
  298.         
  299.         error=Gestalt(gestaltAddressingModeAttr,&value);
  300.         can32=!error && (value&(1<<gestalt32BitCapable));
  301.         
  302.         // RectToAddress computes pixelPtr and clips r to the bit/pixmap bounds.
  303.         pixelPtr=RectToAddress(pmPtr,&r,&rowBytes,&pixelSize,&bitsOffset);
  304.         if(pixelPtr==NULL){
  305.             oldPmPtr=(PixMapPtr)-1;    // invalidate cache
  306.             return 0;
  307.         }
  308.         oldPmPtr=pmPtr;
  309.         oldMap=*(BitMap *)pmPtr;
  310.         logPixelSize=Log2L(pixelSize);
  311.         value+=r.left-x;    // Update after clipping.
  312.         x=r.left;
  313.         n=r.right-r.left;
  314.     }else{
  315.         // Cache is fresh. Merely correct for changes in x and y.
  316.         if(pixelPtr==NULL)return 1;
  317.         value+=r.left-x;    // Update after clipping.
  318.         x=r.left;
  319.         n=r.right-r.left;
  320.         if(x!=oldX){
  321.             if(logPixelSize<3){
  322.                 register long bits;
  323.                 bits=bitsOffset+(long)(x-oldX)<<logPixelSize;
  324.                 pixelPtr+=bits>>3;
  325.                 bitsOffset=bits&7;
  326.             }else pixelPtr+=(x-oldX)<<(logPixelSize-3);
  327.         }
  328.         if(y!=oldY)pixelPtr+=(long)(y-oldY)*rowBytes;
  329.     }
  330.     if(!MATLAB && can32)SwapMMUMode(&mode32);
  331.     switch(logPixelSize){
  332.     case 0:{
  333.         register unsigned char val,mask,*ptr=pixelPtr;
  334.         shift=sizeof(*ptr)*8;
  335.         shift-=bitsOffset;    // from right, instead of from left
  336.         if(MATLAB && can32)SwapMMUMode(&mode32);
  337.         do{
  338.             val=mask=0;
  339.             do{
  340.                 shift-=1;
  341.                 val<<=1;
  342.                 mask<<=1;
  343.                 if(n>0){
  344.                     val|=(*value++)&1;
  345.                     mask|=1;
  346.                     n--;
  347.                 }else{
  348.                     val<<=shift;
  349.                     mask<<=shift;
  350.                     break;
  351.                 }
  352.             }while(shift>0);
  353.             mask=~mask;
  354.             *ptr= *ptr & mask | (unsigned char)val;
  355.             ptr++;
  356.             shift=sizeof(*ptr)*8;
  357.         }while(n>0);
  358.         if(MATLAB && can32)SwapMMUMode(&mode32);
  359.         break;
  360.     }
  361.     case 1:{
  362.         register unsigned char val,mask,*ptr=pixelPtr;
  363.         shift=sizeof(*ptr)*8;
  364.         shift-=bitsOffset;    // from right, instead of from left
  365.         if(MATLAB && can32)SwapMMUMode(&mode32);
  366.         do{
  367.             val=mask=0;
  368.             do{
  369.                 shift-=2;
  370.                 val<<=2;
  371.                 mask<<=2;
  372.                 if(n>0){
  373.                     val|=(*value++)&3;
  374.                     mask|=3;
  375.                     n--;
  376.                 }else{
  377.                     val<<=shift;
  378.                     mask<<=shift;
  379.                     break;
  380.                 }
  381.             }while(shift>0);
  382.             mask=~mask;
  383.             *ptr= *ptr & mask | (unsigned char)val;
  384.             ptr++;
  385.             shift=sizeof(*ptr)*8;
  386.         }while(n>0);
  387.         if(MATLAB && can32)SwapMMUMode(&mode32);
  388.         break;
  389.     }
  390.     case 2:{
  391.         register unsigned char val,mask,*ptr=pixelPtr;
  392.         shift=sizeof(*ptr)*8;
  393.         shift-=bitsOffset;    // from right, instead of from left
  394.         if(MATLAB && can32)SwapMMUMode(&mode32);
  395.         do{
  396.             val=mask=0;
  397.             do{
  398.                 shift-=4;
  399.                 val<<=4;
  400.                 mask<<=4;
  401.                 if(n>0){
  402.                     val|=(*value++)&15;
  403.                     mask|=15;
  404.                     n--;
  405.                 }else{
  406.                     val<<=shift;
  407.                     mask<<=shift;
  408.                     break;
  409.                 }
  410.             }while(shift>0);
  411.             mask=~mask;
  412.             *ptr= *ptr & mask | (unsigned char)val;
  413.             ptr++;
  414.             shift=sizeof(*ptr)*8;
  415.         }while(n>0);
  416.         if(MATLAB && can32)SwapMMUMode(&mode32);
  417.         break;
  418.     }
  419.     case 3:{
  420.         register unsigned char *pB=pixelPtr;
  421.         if(MATLAB && can32)SwapMMUMode(&mode32);
  422.         for(;n>0;n--) *pB++ = *value++;
  423.         if(MATLAB && can32)SwapMMUMode(&mode32);
  424.         break;
  425.     }
  426.     case 4:{
  427.         register unsigned short *pW=(unsigned short *)pixelPtr;
  428.         if(MATLAB && can32)SwapMMUMode(&mode32);
  429.         for(;n>0;n--) *pW++ = *value++;
  430.         if(MATLAB && can32)SwapMMUMode(&mode32);
  431.         break;
  432.     }
  433.     case 5:{
  434.         register unsigned long *pL=(unsigned long *)pixelPtr;
  435.         if(MATLAB && can32)SwapMMUMode(&mode32);
  436.         for(;n>0;n--) *pL++ = *value++;
  437.         if(MATLAB && can32)SwapMMUMode(&mode32);
  438.         break;
  439.     }
  440.     }
  441.     if(!MATLAB && can32)SwapMMUMode(&mode32);
  442.     oldX=x;
  443.     oldY=y;
  444.     return error;
  445. }
  446.  
  447. #if MATLAB
  448. int GetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  449.     ,register short n)
  450. {
  451.   int (*goSet) (PixMapPtr pmPtr,int x,int y,unsigned long value[],register short n);
  452.   int GPixmapPixelsQuickly (PixMapPtr pmPtr,int x,int y,unsigned long value[],register short n);
  453.  
  454.   goSet = (void *) StripAddress(&GPixmapPixelsQuickly);
  455.   return( (*goSet) (pmPtr,x,y,value,n) );
  456. }
  457. static int GPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  458.     ,register short n)
  459. #else
  460. int GetPixmapPixelsQuickly(PixMapPtr pmPtr,int x,int y,unsigned long value[]
  461.     ,register short n)
  462. #endif
  463. // Peeks a rows of pixels of any size. Accepts either pixmap or bitmap pointer.
  464. // (x,y) is in the coordinate system of the bit/pixmap.
  465. // Speed is enhanced by reusing the cached information from last time if it's the
  466. // same Pix/Bitmap as last time, i.e. same address, baseAddr,rowBytes, and bounds.
  467. // You can force it to flush its cache by passing a NULL bit/pixmap pointer.
  468. {
  469.     static PixMapPtr oldPmPtr=(PixMapPtr)-1;
  470.     static int oldX,oldY;
  471.     static short rowBytes,logPixelSize,bitsOffset;
  472.     static unsigned char *pixelPtr;
  473.     static BitMap oldMap;
  474.     static Ptr mainBaseAddr=NULL;
  475.     static Boolean can32=0;
  476.     char mode32=true32b;
  477.     int shift,error=0;
  478.     unsigned char mask;
  479.     Rect r;
  480.     
  481.     // Clip to pix/bitmap bounds.
  482.     SetRect(&r,x,y,x+n,y+1);
  483.     if(mainBaseAddr==NULL){
  484.         if(QD8Exists())mainBaseAddr=(*(*GetMainDevice())->gdPMap)->baseAddr;
  485.         else mainBaseAddr=(void *)-1;
  486.     }
  487.     // Clip unless the pixmap pretends to be the main device.
  488.     if(pmPtr!=NULL && pmPtr->baseAddr!=mainBaseAddr 
  489.         && !SectRect(&pmPtr->bounds,&r,&r))return 1;    // go home if we're done
  490.  
  491.     if(!USE_CACHE
  492.         || pmPtr->baseAddr==mainBaseAddr // tricky, don't trust cache
  493.         || pmPtr!=oldPmPtr
  494.         || pmPtr->baseAddr!=oldMap.baseAddr 
  495.         || pmPtr->rowBytes!=oldMap.rowBytes 
  496.         || *(long *)&pmPtr->bounds.top!=*(long *)&oldMap.bounds.top
  497.         || *(long *)&pmPtr->bounds.bottom!=*(long *)&oldMap.bounds.bottom){
  498.         // Cache is stale. Get fresh values.
  499.         short pixelSize;
  500.         long value=0;
  501.  
  502.         error=Gestalt(gestaltAddressingModeAttr,&value);
  503.         can32=!error && (value&(1<<gestalt32BitCapable));
  504.  
  505.         // RectToAddress computes pixelPtr and clips r to the bit/pixmap bounds.
  506.         pixelPtr=RectToAddress(pmPtr,&r,&rowBytes,&pixelSize,&bitsOffset);
  507.         if(pixelPtr==NULL){
  508.             oldPmPtr=(PixMapPtr)-1;    // invalidate cache
  509.             return 0;
  510.         }
  511.         oldPmPtr=pmPtr;
  512.         oldMap=*(BitMap *)pmPtr;
  513.         logPixelSize=Log2L(pixelSize);
  514.         if(x!=r.left || x+n!=r.right){    // Update after clipping.
  515.             error=1;
  516.             value+=r.left-x;
  517.             x=r.left;
  518.             n=r.right-r.left;
  519.         }
  520.     }else{
  521.         // Cache is fresh. Merely correct for changes in x and y.
  522.         if(pixelPtr==NULL)return 1;
  523.         if(x!=r.left || x+n!=r.right){    // Update after clipping.
  524.             error=1;
  525.             value+=r.left-x;
  526.             x=r.left;
  527.             n=r.right-r.left;
  528.         }
  529.         if(x!=oldX){
  530.             if(logPixelSize<3){
  531.                 register long bits;
  532.                 bits=bitsOffset+(long)(x-oldX)<<logPixelSize;
  533.                 pixelPtr+=bits>>3;
  534.                 bitsOffset=bits&7;
  535.             }else pixelPtr+=(x-oldX)<<(logPixelSize-3);
  536.         }
  537.         if(y!=oldY)pixelPtr+=(long)(y-oldY)*rowBytes;
  538.     }
  539.     if(!MATLAB && can32)SwapMMUMode(&mode32);
  540.     switch(logPixelSize){
  541.     case 0:{
  542.         register unsigned char val,*ptr=pixelPtr;
  543.         shift=sizeof(*ptr)*8;
  544.         shift-=bitsOffset;    // from right, instead of from left
  545.         if(MATLAB && can32)SwapMMUMode(&mode32);
  546.         do{
  547.             val=*ptr++;
  548.             do{
  549.                 shift-=1;
  550.                 if(n>0){
  551.                     *value++=(val>>shift)&1;
  552.                     n--;
  553.                 }else{
  554.                     break;
  555.                 }
  556.             }while(shift>0);
  557.             shift=sizeof(*ptr)*8;
  558.         }while(n>0);
  559.         if(MATLAB && can32)SwapMMUMode(&mode32);
  560.         break;
  561.     }
  562.     case 1:{
  563.         register unsigned char val,*ptr=pixelPtr;
  564.         shift=sizeof(*ptr)*8;
  565.         shift-=bitsOffset;    // from right, instead of from left
  566.         if(MATLAB && can32)SwapMMUMode(&mode32);
  567.         do{
  568.             val=*ptr++;
  569.             do{
  570.                 shift-=2;
  571.                 if(n>0){
  572.                     *value++=(val>>shift)&3;
  573.                     n--;
  574.                 }else{
  575.                     break;
  576.                 }
  577.             }while(shift>0);
  578.             shift=sizeof(*ptr)*8;
  579.         }while(n>0);
  580.         if(MATLAB && can32)SwapMMUMode(&mode32);
  581.         break;
  582.     }
  583.     case 2:{
  584.         register unsigned char val,*ptr=pixelPtr;
  585.         shift=sizeof(*ptr)*8;
  586.         shift-=bitsOffset;    // from right, instead of from left
  587.         if(MATLAB && can32)SwapMMUMode(&mode32);
  588.         do{
  589.             val=*ptr++;
  590.             do{
  591.                 shift-=4;
  592.                 if(n>0){
  593.                     *value++=(val>>shift)&15;
  594.                     n--;
  595.                 }else{
  596.                     break;
  597.                 }
  598.             }while(shift>0);
  599.             shift=sizeof(*ptr)*8;
  600.         }while(n>0);
  601.         if(MATLAB && can32)SwapMMUMode(&mode32);
  602.         break;
  603.     }
  604.     case 3:{
  605.         register unsigned char *pB=pixelPtr;
  606.         if(MATLAB && can32)SwapMMUMode(&mode32);
  607.         for(;n>0;n--) *value++=*pB++;
  608.         if(MATLAB && can32)SwapMMUMode(&mode32);
  609.         break;
  610.     }
  611.     case 4:{
  612.         register unsigned short *pW=(unsigned short *)pixelPtr;
  613.         if(MATLAB && can32)SwapMMUMode(&mode32);
  614.         for(;n>0;n--) *value++=*pW++;
  615.         if(MATLAB && can32)SwapMMUMode(&mode32);
  616.         break;
  617.     }
  618.     case 5:{
  619.         register unsigned long *pL=(unsigned long *)pixelPtr;
  620.         if(MATLAB && can32)SwapMMUMode(&mode32);
  621.         for(;n>0;n--) *value++=*pL++;
  622.         if(MATLAB && can32)SwapMMUMode(&mode32);
  623.         break;
  624.     }
  625.     }
  626.     if(!MATLAB && can32)SwapMMUMode(&mode32);
  627.     oldX=x;
  628.     oldY=y;
  629.     return error;
  630. }
  631.